package com.jfcloud.mall.product.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jfcloud.common.http.RespDataT;
import com.jfcloud.mall.product.domain.JfcloudBanner;
import com.jfcloud.mall.product.domain.JfcloudProductType;
import com.jfcloud.mall.product.domain.dto.IndexVo;
import com.jfcloud.mall.product.domain.dto.ProductListVo;
import com.jfcloud.mall.product.domain.dto.ProductQueryForm;
import com.jfcloud.mall.product.mapper.ProductMapper;
import com.jfcloud.mall.product.persistent.JfcloudBannerService;
import com.jfcloud.mall.product.persistent.JfcloudProductTypeService;

import io.swagger.annotations.ApiOperation;

//已废弃
//@Api(tags = "商城首页API")
//@Controller
//@RequestMapping("/index")
public class IndexController {

	private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);
	
	@Autowired
	private ProductMapper productMapper;
	
	@Autowired
	private JfcloudProductTypeService jfcloudProductTypeService;
	
	@Autowired
	private JfcloudBannerService jfcloudBannerService;
	
	@ApiOperation("商城首页数据")
	@GetMapping(path = "/main")
	@ResponseBody
	public RespDataT<IndexVo> index() {
		try{
			
			IndexVo respDatas= new IndexVo();
			
			respDatas.setTypes(jfcloudProductTypeService.list(
					new QueryWrapper<JfcloudProductType>()
					.eq("status", 1)
					.orderByAsc("sort_Num")));
			
			respDatas.setBanners(jfcloudBannerService.list(
					new QueryWrapper<JfcloudBanner>()
					.eq("status", 1)
					.orderByAsc("sort_Num")));
			
			ProductQueryForm query = new ProductQueryForm();
			Page<ProductListVo> page = new Page<>(1,12);
			IPage<ProductListVo> datas = productMapper.getProductPage(page,query);
			
			respDatas.setProducts(datas.getRecords());
			
			return new RespDataT<IndexVo>(respDatas);
		}catch (Exception e) {
			LOGGER.error(e.getMessage(),e);
			return new RespDataT(e);
		}
	}
	
	@ApiOperation("商城频道页")
	@GetMapping(path = "/list")
	@ResponseBody
	public RespDataT<IPage<ProductListVo>> list(ProductQueryForm query) {
		try{
			
			Page<ProductListVo> page = new Page<>(query.getPage(),query.getPageSize());
			IPage<ProductListVo> datas = productMapper.getProductPage(page,query);
			
			return new RespDataT<IPage<ProductListVo>>(datas);
		}catch (Exception e) {
			LOGGER.error(e.getMessage(),e);
			return new RespDataT(e);
		}
	}
}
